import { WeaTableNew, WeaForm } from 'comsMobx';
import { WeaLocaleProvider } from 'ecCom';
import { observable, action, runInAction } from 'mobx';
import { Base } from '../base';
import * as API from '../../apis/withdraw/withDrawApi';

const getLabel = WeaLocaleProvider.getLabel;
const { TableStore } = WeaTableNew;

export default class ManagerWithDrawStore extends Base {
	form = new WeaForm();
	condition;
	refid = 'widthManager';

	/**
	 * 管理的表格store
	 * @type {{}}
	 */
	@observable tableStore = {}
	/**
	 * 需要提交的参数
	 * @type {{doneSk: Array, todoSk: Array, myTodo: string}}
	 */
	@observable submitparams = {
		todoSk: [],
	};
	/**
	 * 是否加载中
	 * @type {boolean}
	 */
	@observable load = false;
	@observable loadSignInput = false;

	/**
	 * 核心参数从后台获取
	 * @type {{isManager: boolean, showSignInput: boolean, signInputRequired: boolean, showMyTodo: boolean, showDone: boolean, showTodo: boolean, title: string}}
	 */
	coreparams = {
		showSignInput: true,
		signInputRequired: true,
		showTodo: true,
		title: getLabel(32025, '撤回'),
	}
	type = 'manager';

	workparams = {};
	/**
	 * 初始化
	 * @param params
	 * @returns {Promise<void>}
	 */
	@action
	initLoad = async (params = {}) => {
		this.submitparams = [];
		params = { ...params, type: this.type };
		this.workparams = params;
		this.reloadLoad();
		this.getSignInput(params);
	}

	reloadLoad = (reload = false) => {
		let params = { ...this.workparams, ...this.coreparams, ...this.form.getFormParams() };
		API.loadWithDrawData(params).then((res) => {
			const { api_status = false, status = false, msg = '' } = res;
			if (api_status || status) {
				this.doInit(res, reload);
			} else {
				this.showErrorMsg(msg || getLabel(126400, '获取数据失败稍后重试！'));
			}
		});
	}
	getSignInput = () => {
		this.loadSignInput = false;
		API.getSignInputData(this.workparams).then((datas) => {
			runInAction(() => {
				this.loadSignInput = true;
				this.signinput = { ...datas };
			});
		});
	}

	/**
	 * 处理初始化的动作
	 * @param params
	 */
	doInit = (params, reload = false) => {
		const {
			showSignInput = true, signInputRequired = true,
			showTodo = true, title = getLabel(32025, '撤回'),
			todosessinkey = '',
			requestid,
			conditions,
		} = params;
		let todoTableStore = (new TableStore()).getDatas(todosessinkey);

		this.coreparams = { showSignInput, signInputRequired, showTodo, title, requestid };
		this.tableStore = { todoTableStore };
		this.load = true;
		if (!reload) {
			this.condition = conditions;
			this.form.initFormFields(conditions);
		}
	}

	/**
	 * 提交参数的改变
	 * @param params
	 */
	@action
	changeSubmitparams = (params) => {
		this.submitparams = { ...this.submitparams, ...params };
	}

	/**
	 * 撤回准备
	 * TODO
	 */
	goWithDraw = (callback) => {
		const { title, signInputRequired = false, requestid } = this.coreparams;
		const { todoSk = [] } = this.submitparams;
		let { remark } = this.submitparams;
		let isPass = false,
showMsgContent = '';
		// 第一步校验选择项
		// 分管理员还是非管理员
		if (todoSk.length > 0) {
			isPass = true;
		} else {
			showMsgContent = `${getLabel(511633, '请选择要撤回的在办人员')}!`;
		}
		if (!isPass) {
			this.showErrorMsg(showMsgContent, 3);
			return;
		}
		// 第二步校验签字意见
		isPass = false;
		if (signInputRequired) {
			if (window.CK_REF && window.CK_REF[`${this.refid}Edit`]) {
				// remark = window.CK_REF[this.refid + "Edit"].getEditor().getData() || remark;
				remark = window.CK_REF[`${this.refid}Edit`].getEditor().getData();
			}
			if (remark && remark.length > 0) {
				this.submitparams.remark = remark;
				isPass = true;
			} else {
				showMsgContent = getLabel(521596, '请填写$title$意见！');
				showMsgContent = showMsgContent.replace('$title$', title);
			}
		}
		if (!isPass) {
			this.showErrorMsg(showMsgContent, 3);
			return;
		}
		// 第三步用户选择是否确定撤回

		// 第四步：执行撤回请求
		this.doWithDraw({ requestid }, callback);
	}

	/**
	 * 真正的撤回接口
	 * @param params 必须含有流程id【requestid】
	 */
	doWithDraw = (params = {}, callback) => {
		params = { ...params, ...this.submitparams, ...this.workparams };
		API.doWithDraw(params).then((res) => {
			const { api_status = false, status = false, api_message = '' } = res;
			if (api_status || status) {
				this.showSuccessMsg(api_message || getLabel(384408, '撤回成功！'), 3, () => {
					typeof callback == 'function' && (callback(res));
				});
			} else {
				this.showErrorMsg(api_message || getLabel(384409, '撤回失败！'), 3);
			}
		});
	}
	/**
	 * 关闭销毁方法
	 */
	destory = () => {
		this.submitparams = [];
		this.form.resetForm();
	}
}
